package com.shiyi.system.utils;

import com.shiyi.core.utils.encryption.EncryptUtils;
import com.shiyi.core.vo.TokenUser;
import com.shiyi.system.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author shiyi
 * @version 1.0
 * @date 2020-12-7 17:03
 */
@Component
public class UserManager {

    @Autowired
    private RedisCache redisCache;

    /**
     * 根据token从redis中获取用户信息
     */
    public TokenUser getUserByToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        String decodeAccessToken = EncryptUtils.decodeBase64(token);
        return redisCache.getCacheObject(decodeAccessToken);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    public boolean loginOutUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        String decodeBase64 = EncryptUtils.decodeBase64(token);
        redisCache.deleteObject(decodeBase64);
        return true;
    }
}
